/*jslint sloppy: true */
/*global require */
var LM = require('../global');
var angular = require('angular');
require('./detail.html');

LM.controller('Project', function ($scope, Project, $routeParams, Token, $location) {
	$scope.queryConfig.clear().setActive();
	$scope.navConfig.setHref('#/project').setType('back');

	$scope.projectData = { name: '', comment: '', version: '', probePath: '' };
	$scope.project = Project.get({
		projectId: $routeParams.projectId
	}, function () {
		angular.extend($scope.projectData, $scope.project);
	});

	$scope.updateProject = function () {
		angular.extend($scope.project, $scope.projectData);
		$scope.project.$modify();
	};
	$scope.removeProject = function () {
		$scope.project.$remove(null, function () {
			$location.url('/project');
		});
	};
	$scope.removeProjectEnter = function ($event) {
		if ($scope.deleteWord === 'DELETE') {
			$scope.removeProject();
		}
	};

	$scope.token = Token.get({
		projectId: $routeParams.projectId
	});

	$scope.removeToken = function () {
		$scope.token.$remove();
	};

	$scope.createToken = function () {
		var twoWeeksTime = 3600 * 24 * 1000 * 14;
		$scope.token.expiredtime = Date.now() + twoWeeksTime;
		$scope.token.projectId = $routeParams.projectId;
		$scope.token.$create();
	};
});
